From 6b54894503af2e1ae1a7ecdd9a6c0934586ce355 Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Sun, 4 Feb 2024 21:12:49 -0800
Subject: fix(badges): batch import single identity

---
 src/routes/user/[user]/badges/+page.svelte | 34 ++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 9 deletions(-)

(limited to 'src/routes/user/[user]/badges/+page.svelte')

diff --git a/src/routes/user/[user]/badges/+page.svelte b/src/routes/user/[user]/badges/+page.svelte
index 523f9030..96c12e5a 100644
--- a/src/routes/user/[user]/badges/+page.svelte
+++ b/src/routes/user/[user]/badges/+page.svelte
@@ -377,15 +377,26 @@
 	};
 
 	const importBadges = () =>
-		importImages?.forEach((image) => {
-			badgesPromise = fetch(
-				`/api/badges?image=${encodeURIComponent(image.image)}&post=${encodeURIComponent(
-					image.link || '#'
-				)}${importCategory.length > 0 ? `&category=${encodeURIComponent(importCategory)}` : ''}`,
-				{
-					method: 'PUT'
-				}
-			);
+		fetch(
+			`/api/badges?import=true
+			${importCategory.length > 0 ? `&category=${encodeURIComponent(importCategory)}` : ''}
+		`,
+			{
+				method: 'PUT',
+				headers: {
+					'Content-Type': 'application/json'
+				},
+				body: JSON.stringify(
+					importImages?.map((image) => ({
+						image: image.image,
+						post: image.link || '#',
+						category: importCategory
+					}))
+				)
+			}
+		).then(() => {
+			importMode = false;
+			importImages = undefined;
 		});
 </script>
 
@@ -723,6 +734,11 @@
 				<button on:click={() => importBadges()} class="button-lined no-shadow">
 					{$locale().user.badges.importMode.import}
 				</button>
+
+				<SettingHint lineBreak>
+					Please wait until import mode automatically disables after importing, otherwise you risk
+					skipping or duplicating badges.
+				</SettingHint>
 			{/if}
 		</div>
 	</div>
-- 
cgit v1.2.3

